//
//  PPAlbumContentOrderViewController.m
//  pickpic
//
//  Created by Jinxiao on 9/11/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumContentOrderViewController.h"

#import "PPAlbumContentViewDataSource.h"

@interface PPAlbumContentOrderViewController () <UITableViewDelegate, UITableViewDataSource>
@property (readwrite, nonatomic, strong) NSNumber *order;
@property (readwrite, nonatomic, strong) PPAbstractTableView *tableView;
@end

@implementation PPAlbumContentOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = YES;
    self.view.backgroundColor = WHITE;
    _tableView = [[PPAbstractTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];


//    UIView *headerView = [[UIView alloc] init];
//    headerView.backgroundColor = [UIColor clearColor];
//    headerView.frame = CGRectMake(0, 0, CGFLOAT_MIN, 16);
//    _tableView.tableHeaderView = headerView;

//    UIView *footerView = [[UIView alloc] init];
//    footerView.backgroundColor = [UIColor clearColor];
//    footerView.frame = CGRectMake(0, 0, CGFLOAT_MIN, CGFLOAT_MIN);
//    _tableView.tableFooterView = footerView;

    __weak typeof(self)weakSelf = self;
//    [self setNavigationLeftTitle:@"取消" action:^{
//        [weakSelf.navigationController popViewControllerAnimated:YES];
//    }];
    
    
    [self setupBackItem];

    [self setNavigationRightTitle:@"完成" titleColor:COLORA(0, 118, 255) action:^{
        weakSelf.dataSource.order = weakSelf.order;
        
        if (weakSelf.BlockFinsh) {
            weakSelf.BlockFinsh([weakSelf.order integerValue]);
        }
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    _tableView.frame = self.view.bounds;
}

- (void)setDataSource:(PPAlbumContentViewDataSource *)dataSource {
    _dataSource = dataSource;

    _order = dataSource.order;

    [_tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIControl *headerView = [[UIControl alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    headerView.frame = CGRectMake(0, 0, tableView.width, [self tableView:tableView heightForHeaderInSection:section]);

//    [headerView addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [headerView addBottomX:14 BorderWithColor:kLineColor andWidth:kLineWidth];

    NSString *text = nil;
    PPAlbumOrder order = PPAlbumOrderNone;

    switch(section) {
        case 0: order = PPAlbumOrderDesc; text = @"正序"; break;

        case 1: order = PPAlbumOrderAsc;text = @"倒序"; break;

        default: break;
    }

    if(order == _order.integerValue) {
        UIImageView *indicatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Check"]];
//        indicatorView.centerY = headerView.height/2;
//        indicatorView.right = 25;
        indicatorView.frame = CGRectMake(headerView.frame.size.width - 25 - 13, 0, 13, headerView.height);
        indicatorView.contentMode = UIViewContentModeScaleAspectFit;
        [headerView addSubview:indicatorView];
    }

    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.textColor  = COLORA(3, 3, 3);
    label.font = [UIFont boldSystemFontOfSize:16];
    [label sizeToFit];
    label.left = 14;
    label.centerY = headerView.height/2;
    [headerView addSubview:label];

    __weak typeof(self)weakSelf = self;
    [headerView setActionBlock:^(UIControl *control) {
        [weakSelf headerView:control withIndex:section orderDidPressed:order];
    } forControlEvents:UIControlEventTouchUpInside];

    return headerView;
}

- (void)headerView:(UIControl *)headerView withIndex:(NSInteger)index orderDidPressed:(PPAlbumOrder)order {
    _order = @(order);
    
    [_tableView reloadData];
}

@end
